var userida = sessionStorage.getItem('user');
function write_new(posts){
    const postContainer = document.querySelector('.post-container');
    postContainer.innerHTML = ''; 
    posts.forEach((post) => {
        const postElement = document.createElement('div');
        postElement.className = 'post';
        postElement.innerHTML = `${post.phead}` + '<br>' + `${post.pbody.substring(0, 15)}`; 
        postElement.onclick = () => {
            window.location.href = `tlsq-xq.php?postid=${post.id}`; 
        };
        postContainer.appendChild(postElement);
    });
}
fetch('book/post.php')
.then(response => response.json())
.then(data => {
    write_new(data);
});
function searchpost() {
    const searchvalue = document.getElementById('searchBox').value;
    fetch(`book/post-search.php?search=${searchvalue}`)
    .then(response => response.json())
    .then(data => {
        if(data.msg === 400){
            Qmsg.error('没有找到相关帖子');
            return;
        }
        write_new(data.retu);
    });
}

    var editor;
    KindEditor.ready(function(K) {
        editor = K.create('textarea[name="content"]', {
            resizeType : 1,
            allowPreviewEmoticons : false,
            allowImageUpload : false,
            items : [
                'fontname', 'fontsize', '|', 'forecolor', 'hilitecolor', 'bold', 'italic', 'underline',
                'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                'insertunorderedlist', '|', 'emoticons', 'image', 'link']
        });
    });
    function outpost(){
        if(userida === null){
            Qmsg.error('请先登录');
            setTimeout(function(){
                window.location.href = '/center-person/lgreg.php';
            }, 1500);
            return;
        }
        if(document.getElementById('title_post').value === ''||editor.html() === ''){
            Qmsg.error('标题或内容不能为空');
            return;
        }
        if(!confirm('确定发布帖子？')){
            return;
        }
        fetch('book/post-add.php', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            },
            body: JSON.stringify({
                title: document.getElementById('title_post').value,
                content: editor.html(),
                userid:userida
            })
        }).then(response => response.json())
        .then(data => {
            if(data.code === 400){
                Qmsg.error(data.msg);
            }else{
            Qmsg.success(data.msg);
        }
            document.getElementById('title_post').value = '';
            editor.html('');
        });
    }
    function addpost(){
        if(document.getElementById('post_add').style.visibility === 'hidden'){
            document.getElementById('post_add').style.visibility = 'visible';
            document.getElementById('post_rangs').style.visibility = 'hidden';
            document.getElementById('creadt').innerHTML = '阅帖';
        }else{
            document.getElementById('post_add').style.visibility = 'hidden'
            document.getElementById('post_rangs').style.visibility = 'visible';
            document.getElementById('creadt').innerHTML = '发帖';
        }
    }